package com.lwq.commentTool.view;

import java.awt.Choice;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyledDocument;

import com.lwq.commentTool.model.EditInformModel;
import com.lwq.commentTool.model.EditItemInformModel;
import com.lwq.commentTool.service.EditInformService;
import com.lwq.commentTool.util.Constance;
import com.lwq.commentTool.util.SqlUtil;

public class EditBoard extends FrameView {
	private JScrollPane editScrollJPanel = null;
	private JPanel editJPanel = null;
	private JScrollPane centerJPanel = null;
	private StyledDocument document;
	private GridBagConstraints mGridBagConstraints = null;
	private GridBagLayout mGridBagLayout = null;
	private JTextPane mJTextPane = null;
	private JButton mUpdateBtn = null;
	private EditInformModel mInfoModel = null;

	private Map<String, Component> mComponentMap = null;

	public EditBoard(String lable, int width, int height) {
		super(lable, width, height);
		mComponentMap = new HashMap<String, Component>();
		mGridBagLayout = new GridBagLayout();
		mGridBagConstraints = new GridBagConstraints();
		mGridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
		editJPanel = new JPanel();
		editScrollJPanel = new JScrollPane(editJPanel);
		add(editScrollJPanel, "East");
		editJPanel.setLayout(mGridBagLayout);

		// text
		mJTextPane = new JTextPane();
		document = mJTextPane.getStyledDocument();
		centerJPanel = new JScrollPane(mJTextPane);
		documentSetText("test");
		add(centerJPanel, "Center");

		// button
		mUpdateBtn = new JButton("create");
		initEditBoard();
		setEvent();
	}

	private void initEditBoard() {
		int posY = 1;
		EditItemInformModel item = null;
		mInfoModel = EditInformService.getInstance().getEditInformModel();
		addComponent(editJPanel, mGridBagLayout, mGridBagConstraints,
				mUpdateBtn, 0, 0, 1, 1);
		if (mInfoModel != null) {
			for (int i = 0; i < mInfoModel.getItemCount(); i++) {
				item = mInfoModel.getItem(i);
				switch (item.getType()) {
				case Constance.EDITITEM_CHOICE_TYPE:
					addComponent(editJPanel, mGridBagLayout,
							mGridBagConstraints,
							getChoice(item.getLable(), item.getValue()), 0,
							posY, 1, 1);
					break;
				case Constance.EDITITEM_TEXT_TYPE:
					addComponent(editJPanel, mGridBagLayout,
							mGridBagConstraints,
							getTextField(item.getLable(), item.getValue()), 0,
							posY, 1, 1);
					break;
				}
				posY++;
			}
		}
	}

	private void documentSetText(String text) {
		document = new DefaultStyledDocument();
		mJTextPane.setStyledDocument(document);
		try {
			document.insertString(document.getLength(), text + "\n", null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		mJTextPane.setCaretPosition(mJTextPane.getDocument().getLength());
	}

	private void documentInsertText(String text) {
		try {
			document.insertString(document.getLength(), text + "\n", null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		mJTextPane.setCaretPosition(mJTextPane.getDocument().getLength());
	}

	private void setEvent() {
		mUpdateBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String result = null;
				EditItemInformModel item = null;
				Choice ch = null;
				JTextField jt = null;
				for (int i = 0; i < mInfoModel.getItemCount(); i++) {
					item = mInfoModel.getItem(i);
					switch (item.getType()) {
					case Constance.EDITITEM_CHOICE_TYPE:
						ch = (Choice) mComponentMap.get(item.getLable());
						if (ch != null) {
							item.setReslut(ch.getSelectedItem());
						}
						break;
					case Constance.EDITITEM_TEXT_TYPE:
						jt = (JTextField) mComponentMap.get(item.getLable());
						if (jt != null) {
							item.setReslut(jt.getText());
						}
						break;
					}
				}
				result = EditInformService.getInstance().getProccString(mInfoModel);
				System.out.println("result="+result);
				documentSetText(result);
			}
		});
	}

	public JPanel getTextField(String labStr, String value) {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		JLabel lab = new JLabel(labStr);
		p.add(lab);
		JTextField tx = new JTextField(value);
		p.add(tx);
		mComponentMap.put(labStr, tx);
		return p;
	}

	public JPanel getChoice(String labStr, String value) {
		String[] items = value.split("\\|");
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		JLabel lab = new JLabel(labStr);
		p.add(lab);
		Choice ch = new Choice();
		for (int i = 0; i < items.length; i++) {
			ch.add(items[i]);
		}
		p.add(ch);
		mComponentMap.put(labStr, ch);
		return p;
	}

	public JPanel getPanel(JPanel parent, String org, int x, int y) {
		JPanel jp = new JPanel();
		if (parent != null && org != null) {
			parent.add(jp, org);
		}
		return jp;
	}

	public void addComponent(JPanel p, GridBagLayout gb,
			GridBagConstraints gbc, Component c, int x, int y, int width,
			int height) {
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridheight = height;
		gbc.gridwidth = width;
		gb.setConstraints(c, gbc);
		p.add(c);
	}
}
